package com.cskaoyan.controller.system;



import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketAdmin;
import com.cskaoyan.controller.system.annotation.RecordOperation;
import com.cskaoyan.service.system.AdminManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 管理员模块
 */
@RestController
@RequestMapping("admin/admin")
public class AdminManagement {

    @Autowired
    AdminManagementService adminManagementService;


    /**
     * 删除管理员
     * @param admin
     * @return
     */
    @RecordOperation
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketAdmin admin){
        adminManagementService.deleteAdmin(admin);
        return BaseRespVo.succeed();
    }
    /**
     * 修改管理员信息
     * @param admin
     * @return
     * 2022年11月20日16:45:24
     */
    @RecordOperation
    @RequestMapping("update")
    public BaseRespVo<MarketAdmin> update(@RequestBody MarketAdmin admin){
        MarketAdmin marketAdmin = adminManagementService.updateAdmin(admin);
        BaseRespVo<MarketAdmin> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(marketAdmin);
        return baseRespVo;
    }
    /**
     * 增加管理员
     * @return
     */
    @RecordOperation
    @RequestMapping("create")
    public BaseRespVo<MarketAdmin> createAdmin(@RequestBody MarketAdmin admin){
   //响应：{"errno":0,"data":{"id":17,"username":"qweqweqeqweqew","password":"$2a$10$1czRJEUq9nh9Zyprurs4AuFzrYeehsPtw1Lb47p3gvrzF9DUXFnB2","avatar":"http://182.92.235.201:8083/wx/storage/fetch/fyjewt5lojbdy9x0n3hf.jpg","addTime":"2022-11-20 16:09:24","updateTime":"2022-11-20 16:09:24","roleIds":[6,3]},"errmsg":"成功"}
        MarketAdmin admin1 = adminManagementService.insertAdmin(admin);
        BaseRespVo<MarketAdmin> baseResp = new BaseRespVo<>();
        baseResp.setErrno(0);
        baseResp.setErrmsg("成功");
        baseResp.setData(admin1);
        return baseResp;
    }

    /**
     * 展示管理员
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo<CommonData<MarketAdmin>> list(@RequestParam("page")Integer page,
                                                    @RequestParam("limit")Integer limit,
                                                    @RequestParam("sort")String sort,
                                                    @RequestParam("order")String order,
                                                    HttpServletRequest request){
        String username = request.getParameter("username");
        //CommonData<T>
        //MarketAdmin
        CommonData<MarketAdmin> marketAdminCommonData = adminManagementService.selectCommonData(page, limit, sort, order,username);
        BaseRespVo<CommonData<MarketAdmin>> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        baseRespVo.setData(marketAdminCommonData);
        return baseRespVo;
    }

}
